<%@ page contentType="text/html; charset=UTF-8"%>
<%@page import="net.sf.json.JSONArray"%>
<%@page import="net.sf.json.JSONObject"%>
<%@page import="java.util.List"%>
<%@page import="com.sinodata.bsm.common.vo.Res"%>
<%
	JSONArray res = (JSONArray)request.getAttribute("res");
	boolean hasEditResPrivilege = (Boolean)request.getAttribute("hasEditResPrivilege");
 %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>资源树定制</title>
  	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="pragma" content="no-cache"/>
	<meta http-equiv="cache-control" content="no-cache"/>
	<meta http-equiv="expires" content="0"/>  
	<%@ include file="../include/common.jsp"%>
    <%@ include file="../include/table.jsp"%>
    <%@ include file="../include/tree.jsp"%>
    <style type="text/css">
    	#res tbody td{
			white-space:normal;
		}
		.input_text{
			border:1px solid #D6D3D6;
			height: 20px;
			line-height:20px;
			width:180px;
		}
		ul li{
			list-style: none;
			margin:5px;
		}
    </style>
    
    <script type="text/javascript">
    	function changeText(index){
    		var isDisp = $('#isDisp'+index+'');
    		var dispName = $('#dispName'+index+'');
    		if(isDisp.val()==0){
    			dispName.attr('disabled','disabled');
    		}else{
    			dispName.removeAttr('disabled');
    		}
    	}
    	
    	function save(){
    		var isDisps = $("select[name='isDisp']");
    		
    		var dispConfs = '';
    		$.each(isDisps,function(index,ob){
    			var isDisp = $(ob).val();
    			var resId  = $(ob).attr('resId');
    			var resTypeId = $(ob).attr('resTypeId');
    			var dispName = $('#dispName'+index+'').val().replace("#","");
    			dispConfs +=resId+','+resTypeId+','+isDisp+','+dispName+"#";
    		});
    		
    		getTopWin(window).getMessageBox().tip("定制中...", 'loading');
    		$.ajax({
				type:'post',
				url:'res-custom!saveResTypeDispConf.action',
				data:{dispConfs:dispConfs,resId:'${resId}'},
				global:false,
				async:false,
				cache:false,
				dataType:'json',
				success:function(data,textStatus){
					window.setTimeout(function () {
						getTopWin(window).getMessageBox().tip('定制成功,请手动刷新树.', 'success'); 
						window.location.reload();
					},1500);
				},
				error:function(){
					getTopWin(window).getMessageBox().alert('服务器内部错误,请联系管理员','错误');
				}
			});
    		
    	}
    		
    	//上移
    	function up(){
    		//下拉对象
    		var childRes = document.getElementById("childRes");  
    		for	(var i=1; i<childRes.options.length; i++)
			{
				if(childRes.options[i].selected)
				{
					var toption=childRes.options[i];
					childRes.insertBefore(toption, childRes.options[i-1]);
				}
			}
    	}
    	
    	//下移
    	function down(){
    		var childRes = document.getElementById("childRes"); 
			for	(var i=childRes.options.length-2; i>=0; i--)
			{
				if(childRes.options[i].selected)
				{
					var	toption=childRes.options[i];
					childRes.insertBefore(toption, childRes.options[i+2]);
				}
			}
    	}
    	
    	//移至顶层
    	function moveTop(){
    		
		 	var ops = document.getElementById("childRes");
  			var count=0;
  			for	(var i=1; i<ops.options.length; i++)
			{
				if(ops.options[i].selected)
				{
					var toption=ops.options[i];
					ops.insertBefore(toption, ops.options[count]);
					count++;
				}
			}
    	}
    	
    	//移至底层
    	function moveBottom(){
    		var ops = document.getElementById("childRes");
  			var count=ops.options.length;
  			for	(var i=ops.options.length-1; i>=0; i--)
			{
				if(ops.options[i].selected)
				{
					var toption=ops.options[i];
					ops.insertBefore(toption, ops.options[count]);
					count--;
				}
			}
    	}
    	
    	function ok(){
    		var parentdId = window.parent.parent.getResTree().getSelectedNodes()[0].id;
    		var options = $('#childRes option');
    		var ids = '';
    		var name = '';
    		$.each(options,function(index,ob){
    			ids += ob.value;
    			name +=$(ob).text();
    			if(index!=(options.length-1)){
    				ids+='$';
    				name+='$';
    			}
    		});
    		
    		$.ajax({
				type:'post',
				url:'res-custom!saveTreeNodeSequence.action',
				data:{nodeId:parentdId,childIds:ids,name:name},
				global:false,
				async:false,
				cache:false,
				dataType:'json',
				success:function(data,textStatus){
					getTopWin(window).getMessageBox().tip('成功,请手动刷新树.', 'success'); 
				},
				error:function(){
					getTopWin(window).getMessageBox().alert('服务器内部错误,请联系管理员','错误');
				}
			});
    	}
    	
    	$(document).ready(function(){
    		var hasEditPrivalege = '<%=hasEditResPrivilege %>';
    		if(hasEditPrivalege=='false'){
    			$('.btn-4').hide();
    		}
    	});
    </script>
  </head>
  
  <body>
  	<h2 class="title-h2"  ><span><img src="${pageContext.request.contextPath}/style/default/bsm/images/arrow-down.gif"/></span>定制类型显示节点</h2>
    <div >
      <form action="">
    	<table   id="res" cellpadding="0" cellspacing="0" border="0" class="display"  >
			<thead>
    	 		<tr>
    	 			<th width="40%">资源类型</th>
    	 			<th width="90px">当前显示状态</th>
    	 			<th width="40%">类别显示名称</th> 
    	 		</tr>
    	 	</thead>
    	 	<tbody>
    	 		<%
    	 			JSONArray array=(JSONArray)request.getAttribute("dispConfs");
    	 			for(int i=0;i<array.size();i++){
    	 			 JSONObject object = array.getJSONObject(i);
    	 		 %>
	    	 		<tr class="<% if((i+1)%2==0){out.println("even");}else{out.println("odd");} %>" >
	    	 			<td><%=object.get("typeName").toString() %></td>
	    	 			<td>
	    	 				<select name="isDisp" id="isDisp<%=i %>" onchange="changeText(<%=i %>);"   resId="<%=object.get("resId") %>" resTypeId="<%=object.get("resTypeId") %>" >
	                            <option <% if(object.get("isDisp").toString().equals("1")){out.println("selected");} %>  value="1">显示</option>
	                            <option <% if(object.get("isDisp").toString().equals("0")){out.println("selected");} %> value="0">不显示</option>
                        	</select>
	    	 			</td>
	    	 			<td>
	    	 				<input type="text" name="dispName" id="dispName<%=i %>" <% if(object.get("isDisp").toString().equals("0")){out.println("disabled");} %>   value="<%=object.get("name").toString().trim()  %>"  class="input_text" maxlength="20" />
	    	 			</td>
	    	 		</tr>
	    	 	<%
	    	 		}
	    	 	 %>
    	 	</tbody>
    	 </table>
    	 <div class="btn-div">
    		<input type="button" value="提交" class="btn-4" onclick="save()" />
    		<input type="reset" value="重置" class="btn-4" />
    	</div>
      </form>
    </div>
   
	    <h2 class="title-h2" ><span><img src="${pageContext.request.contextPath}/style/default/bsm/images/arrow-down.gif"/></span>树节点上下顺序定制</h2>
	    <div style="padding-top:5px;height:<%=res.size()<=15?280:(res.size()*17) %>px;">
	    	<span style="float:left;height:100%;" > 
	    		<select id="childRes" multiple="multiple" style="width:200px;height:280px;border:1px solid #d8d8d8;;" >
	    			<%
	    				
	    				for(int i=0;res!=null&&i<res.size();i++){
	    				JSONObject r = (JSONObject)res.get(i);
	    			%>
	    				<option value="<%=r.get("id") %>" ><%=r.get("name") %></option>
	    			<%
	    				}
	    			 %>
	    		</select>
	    	</span>
	    	<span style="float:left;padding-top:90px;">
	    		<ul >
	    			<li>
	    				<input type="button" value="移至顶层" onclick="moveTop()" id="top" class="btn-4" />
	    			</li>
	    			<li>
	    				<input type="button" value=" 上 移 &nbsp;" onclick="up()" id="up" class="btn-4" />
	    			</li>
	    			<li>
	    				<input type="button" value=" 下 移 &nbsp;" onclick="down()" id="down"  class="btn-4" />
	    			</li>
	    			<li>
	    				<input type="button" value="移至底层" onclick="moveBottom()" id="bottom"  class="btn-4" />
	    			</li>
	    		</ul>
	    	</span>
	    </div>
	    <div style="margin-top:5px;">
	    	<input type="button" value="确定" id="ok" onclick="ok()"  class="btn-4" />
	    </div>
  </body>
</html>
